//
//  HPMsg.m
//
//  Created by 啊  on 16/4/24
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "HPMsg.h"
//#import "HPMe.h"
#import "HPTask.h"
#import "HPKnowledge.h"
//#import "HPTalk.h"
#import "HPNews.h"


NSString *const kHPMsgMe = @"me";
NSString *const kHPMsgTask = @"task";
NSString *const kHPMsgKnowledge = @"knowledge";
NSString *const kHPMsgTalk = @"talk";
NSString *const kHPMsgNews = @"news";


@interface HPMsg ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation HPMsg

@synthesize me = _me;
@synthesize task = _task;
@synthesize knowledge = _knowledge;
@synthesize talk = _talk;
@synthesize news = _news;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.me = [HPMe modelObjectWithDictionary:[dict objectForKey:kHPMsgMe]];
    NSObject *receivedHPTask = [dict objectForKey:kHPMsgTask];
    NSMutableArray *parsedHPTask = [NSMutableArray array];
    if ([receivedHPTask isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedHPTask) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedHPTask addObject:[HPTask modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedHPTask isKindOfClass:[NSDictionary class]]) {
       [parsedHPTask addObject:[HPTask modelObjectWithDictionary:(NSDictionary *)receivedHPTask]];
    }

    self.task = [NSArray arrayWithArray:parsedHPTask];
    NSObject *receivedHPKnowledge = [dict objectForKey:kHPMsgKnowledge];
    NSMutableArray *parsedHPKnowledge = [NSMutableArray array];
    if ([receivedHPKnowledge isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedHPKnowledge) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedHPKnowledge addObject:[HPKnowledge modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedHPKnowledge isKindOfClass:[NSDictionary class]]) {
       [parsedHPKnowledge addObject:[HPKnowledge modelObjectWithDictionary:(NSDictionary *)receivedHPKnowledge]];
    }

    self.knowledge = [NSArray arrayWithArray:parsedHPKnowledge];
            self.talk = [HPTalk modelObjectWithDictionary:[dict objectForKey:kHPMsgTalk]];
    NSObject *receivedHPNews = [dict objectForKey:kHPMsgNews];
    NSMutableArray *parsedHPNews = [NSMutableArray array];
    if ([receivedHPNews isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedHPNews) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedHPNews addObject:[HPNews modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedHPNews isKindOfClass:[NSDictionary class]]) {
       [parsedHPNews addObject:[HPNews modelObjectWithDictionary:(NSDictionary *)receivedHPNews]];
    }

    self.news = [NSArray arrayWithArray:parsedHPNews];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:[self.me dictionaryRepresentation] forKey:kHPMsgMe];
    NSMutableArray *tempArrayForTask = [NSMutableArray array];
    for (NSObject *subArrayObject in self.task) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForTask addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForTask addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForTask] forKey:kHPMsgTask];
    NSMutableArray *tempArrayForKnowledge = [NSMutableArray array];
    for (NSObject *subArrayObject in self.knowledge) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForKnowledge addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForKnowledge addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForKnowledge] forKey:kHPMsgKnowledge];
    [mutableDict setValue:[self.talk dictionaryRepresentation] forKey:kHPMsgTalk];
    NSMutableArray *tempArrayForNews = [NSMutableArray array];
    for (NSObject *subArrayObject in self.news) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForNews addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForNews addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForNews] forKey:kHPMsgNews];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.me = [aDecoder decodeObjectForKey:kHPMsgMe];
    self.task = [aDecoder decodeObjectForKey:kHPMsgTask];
    self.knowledge = [aDecoder decodeObjectForKey:kHPMsgKnowledge];
    self.talk = [aDecoder decodeObjectForKey:kHPMsgTalk];
    self.news = [aDecoder decodeObjectForKey:kHPMsgNews];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_me forKey:kHPMsgMe];
    [aCoder encodeObject:_task forKey:kHPMsgTask];
    [aCoder encodeObject:_knowledge forKey:kHPMsgKnowledge];
    [aCoder encodeObject:_talk forKey:kHPMsgTalk];
    [aCoder encodeObject:_news forKey:kHPMsgNews];
}

- (id)copyWithZone:(NSZone *)zone
{
    HPMsg *copy = [[HPMsg alloc] init];
    
    if (copy) {

        copy.me = [self.me copyWithZone:zone];
        copy.task = [self.task copyWithZone:zone];
        copy.knowledge = [self.knowledge copyWithZone:zone];
        copy.talk = [self.talk copyWithZone:zone];
        copy.news = [self.news copyWithZone:zone];
    }
    
    return copy;
}


@end
